<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    class Node {
      constructor (data) {
        this.data = data
        this.next = null
      }
    }
    class LinkedList {
      constructor () {
        this.head = null
        this.length = 0
      }
      // 添加节点
      append (data) {
        if (!data || data.length === 0) return console.error('请传入数据')
        // 创建新节点
        const newNode = new Node(data)
        // 判断是否为第一个节点
        if (this.length === 0 ) {
          this.head = newNode
        } else {
          let current = this.head
          // 遍历到最后的current
          while (current.next) {
            current = current.next
          }
          current.next = newNode
        }
        // 长度加1
        this.length++
      }
      // 插入节点
      insert (position, data) {
        // console.log(!(0 <= position && position <= this.length))
        if (!(0 <= position && position <= this.length)) {
          console.error('输入的position不在合法范围内')
          return false
        }
        const newNode = new Node(data)
        let current = this.head
        if (position === 0) {
          this.head = newNode
          newNode.next = current
        } else if (0 < position && position <= this.length) {
          let index = 0
          let previous = null
          while (index++ < position) {
            previous = current
            current = current.next
          }
          previous.next = newNode
          newNode.next = current
        }
        this.length++
        return true
      }
      // 根据index查找节点
      getDataByIndex (position) {
        if (!(0 <= position && position < this.length)) {
          console.error('请输入合法的position')
          return false
        }
        let index = 0
        let current = this.head
        // 遍历到position
        while (index++ < position) {
          current = current.next
        }
        return current.data
      }
      // 根据data查找index
      indexOf (data) {
        let current = this.head
        // 用来记录index
        let index = 0
        while (current.next) {
          if (current.data === data) {
            break
          }
          current = current.next
          index++
        }
        // 如果遍历到最后都没有匹配的数据, 则返回console.log.error
        if (index === this.length - 1 && current.data !== data) {
          return console.error(new Error('请输入存在的data'))
        }
        return index
      }
      removeAt (position) {
        if (!(0 <= position && position < this.length)) {
          console.error('请输入合法的position')
          return false
        }
        let current = this.head
        // 判断是否移除第一项
        if (position === 0) {
          this.head = current.next
        } else {
          let index = 0
          // 记录current的上一项
          let previous = null
          while (index++ < position) {
            previous = current
            current = current.next
          }
          previous.next = current.next
        }
        this.length--
        return current.data
      }
      remove (data) {
        // 获取与该节点匹配的index
        const index = this.indexOf(data)
        // NaN ≠ NaN
        // 当一个数为0的时候也可以要它为true时 || index === 0
        if (index || index === 0) {
          return this.removeAt(index)
        }
        console.error(new Error('无法获取数据'))
      }
      // 展现返回展示数组
      showNodeList () {
        const result = ['head', '->']
        let current = this.head
        while (current) {
          result.push(current.data, '->')
          current = current.next
        }
        result.push('end')
        return result.join(' ')
      }
    }
    const test = new LinkedList()
    test.append(1)
    test.append(2)
    test.append(3)
    test.append(4)
    test.append(5)
    test.append([])
    console.log(test.showNodeList())
  </script>
</body>
</html>